﻿using AbpVnext_Order.DBContext;
using AbpVnext_Order.Model;
using AbpVnext_Order_Contracts.Dto;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace AbpVnext_Order.Repository
{
    /// <summary>
    /// 业务模块切换数据库
    /// </summary>
    [Dependency(ServiceLifetime.Transient)]
    public class OrderRepository : EfCoreRepository<OrderDbContext, Order, int>, IOrderRepository
    {
        public OrderRepository(IDbContextProvider<OrderDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="includeDetails"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override async Task<Order> GetAsync(int id, bool includeDetails = true, CancellationToken cancellationToken = default)
        {
            Order productImage = (await GetQueryableAsync()).First(x => x.Id == id);
            return productImage;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="skipCount"></param>
        /// <param name="maxResultCount"></param>
        /// <param name="sorting"></param>
        /// <param name="includeDetails"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task<List<Order>> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default)
        {

            return base.GetPagedListAsync(skipCount, maxResultCount, sorting, includeDetails, cancellationToken);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="autoSave"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override async Task<Order> UpdateAsync(Order entity, bool autoSave = false, CancellationToken cancellationToken = default)
        {
            OrderDbContext projectDbContext = await GetDbContextAsync();
            projectDbContext.Update(entity);
            //await projectDbContext.SaveChangesAsync(cancellationToken);
            //Order productImage = await base.UpdateAsync(entity);
            await SaveChangesAsync(default);
            return entity;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="autoSave"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override async Task DeleteAsync(int id, bool autoSave = false, CancellationToken cancellationToken = default)
        {
            //Order productImage = (await GetQueryableAsync()).First(x => x.Id == id);
            await DeleteAsync(id);
            await SaveChangesAsync(default);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="productImagePageDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<Order>> GetPagedListAsync(OderPageDto oderPageDto)
        {
            //var context = await GetDbContextAsync();
            var product = (await GetQueryableAsync()).Where(x => x.OrderSn == oderPageDto.OrderSn).Skip(0).Take(10);
            var productCount = product.Count();
            var productList = product.ToList();
            return productList;
        }
    }
}
